package org.de.maeyer.statistic;

import static org.de.maeyer.statistic.DefaultStatistics.*;

import java.util.BitSet;
import java.util.List;

import org.de.maeyer.unit.Unit;

/**
 * @author Peter De Maeyer
 */
public class StatisticsBuilder {
	
	private final String name;
	
	private final Unit unit;
	
	private final Class valueClass;
	
	private final BitSet bits = new BitSet();
	
	private long count;
	
	private Object lastValue;
	
	private Object minimum;
	
	private Object maximum;
	
	private Number total;
	
	private Unit timeUnit;
	
	private long elapsedTime;
	
	private double mean;

	private double residual;
	
	private List<Category> categories;
	
	private int[] modeCategoryIndexes;
	
	public StatisticsBuilder(String name, Unit unit, Class valueClass) {
		this.name = name;
		this.unit = unit;
		this.valueClass = valueClass;
	}

    public void setCount(long count) {
	    this.count = count;
	    bits.set(COUNT_BIT);
    }

    public void setElapsedTime(long elapsedTime, Unit timeUnit) {
		this.elapsedTime = elapsedTime;
		this.timeUnit = timeUnit;
		bits.set(ELAPSED_TIME_BIT);
    }

    public void setLastValue(Object lastValue) {
		this.lastValue = lastValue;
		bits.set(LAST_VALUE_BIT);
    }

    public void setTotal(Number total) {
	    this.total = total;
	    bits.set(TOTAL_BIT);
    }
	
	public void setVariance(long count, double mean, double residual) {
	    this.count = count;
	    this.mean = mean;
	    this.residual = residual;
	    bits.set(COUNT_BIT);
	    bits.set(MEAN_BIT);
	    bits.set(VARIANCE_BIT);
	}
	
	public void setExtrema(Object minimum, Object maximum) {
		this.minimum = minimum;
		this.maximum = maximum;
		bits.set(EXTREMA_BIT);
	}
	
	public void setHistogram(List<Category> categories, int[] modeCategoryIndexes) {
		this.categories = categories;
		this.modeCategoryIndexes = modeCategoryIndexes;
		bits.set(HISTOGRAM_BIT);
	}

    public Statistics toStatistics() {
	    return new DefaultStatistics(valueClass, name, unit, bits, count, lastValue, minimum, maximum, total, timeUnit, elapsedTime, mean, residual, categories, modeCategoryIndexes);
    }
}
